//获取登录用户名
var rename = tool.getCookie('rename')
var user_name = tool.getCookie("username")
if(rename || user_name){
    var res = rename || user_name
    $(".nav .a_login").css("display","none").next().css("display","none")
    $(".nav .a_register").css("display","none").next().css("display","none")
    $(".user_box").css("display","block")
    $(".user_id").text(res)
}
$(".exit").click(function(){
    tool.setCookie('rename',$('#usname').val(),-1000)
    tool.setCookie('username',$('#usname').val(),-1000)
    location.href="./list.html"
})
$(".nav .a_login").click(function(){
    localStorage.setItem("url",location.href)
    location.href="./login.html"
})
$(".a_register").click(function(){
    location.href="./register.html"
})
$(".cart").click(function(){
    location.href="./shopping.html"
})

// 获取数据
var arr = location.search.match(/cat=(\d+)/)
if(!arr){
    var cat = "all"
}else{
    console.log(arr)
    var cat = arr[1]
}
var loadindex = layer.load(0,{
    shade: [0.5,'#666']
})
getData(cat)
function getData(cat){
    $.ajax({
        data:{cat},
        url:"./php/list.php",
        dataType:"json",
        success:res=>{
            var {data}=res;
            if(data.length){
                var pageSize = 15;
                new Page("fenye",{
                    first:"首页",
                    previous:"上一页",
                    next:"下一页",
                    last:"尾页"
                },{
                    total:data.length,
                    pageSize
                },function(currentPage){
                    var arr = data.slice((currentPage-1)*pageSize,currentPage*pageSize);
                    var str = "";
                    arr.forEach(item=>{
                        str+=`
                        <dl>
                        <a href="detail.html?id=${item.id}">
                            <dt><img src="${item.img}" alt="" title="${item.name}"></dt>
                            <dd><p>${item.name}</p><b><span>￥</span>${item.price}</b></dd>
                         </a>
                         </dl>
                        `
                    })
                    $(".list").html(str)
                    layer.close(loadindex)
                })
            }else{
                layer.close(loadindex);
                $(".list").html(`
                    <h1>很抱歉，当前分类下暂无商品！<br><a href="./index.html">返回首页</a></h1>
                `)
            }
        }
    })
}